using System.Collections.Generic;
using System.Net.Mail;

namespace CSW.Framework.Common.Utils.Mail
{
    /// <summary>
    /// Static class for sending e-mail.
    /// </summary>
    public static class Email
    {
        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="smtpServer">The SMTP server.</param>
        /// <param name="from">Who the message appears to be from.</param>
        /// <param name="to">The address to email.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        public static void Send(string smtpServer, string from, string to, string subject, string body)
        {
            Send(smtpServer, from, new[] { to }, subject, body);
        }

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="smtpServer">The SMTP server.</param>
        /// <param name="from">Who the message appears to be from.</param>
        /// <param name="to">The address to email.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="mailFormat">The mail format.</param>
        public static void Send(string smtpServer, string from, string to, string subject, string body, MailFormat mailFormat)
        {
            Send(smtpServer, from, new[] { to }, subject, body, mailFormat, MailPriority.Normal);
        }

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="smtpServer">The SMTP server.</param>
        /// <param name="from">Who the message appears to be from.</param>
        /// <param name="to">The "To" list.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        public static void Send(string smtpServer, string from, ICollection<string> to, string subject, string body)
        {
            Send(smtpServer, from, to, subject, body, MailFormat.Basic);
        }

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="smtpServer">The SMTP server.</param>
        /// <param name="from">Who the message appears to be from.</param>
        /// <param name="to">The "To" list.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="mailFormat">The mail format.</param>
        public static void Send(string smtpServer, string from, ICollection<string> to, string subject, string body, MailFormat mailFormat)
        {
            Send(smtpServer, from, to, subject, body, mailFormat, MailPriority.Normal);
        }

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="smtpServer">The SMTP server.</param>
        /// <param name="from">Who the message appears to be from.</param>
        /// <param name="to">The "To" list.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="priority">The mail priority.</param>
        /// <param name="mailFormat">The mail format.</param>
        public static void Send(string smtpServer, string from, string to, string subject, string body, MailFormat mailFormat, MailPriority priority)
        {
            Send(smtpServer, from, new[] { to }, subject, body, mailFormat, priority, null);
        }

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="smtpServer">The SMTP server.</param>
        /// <param name="from">Who the message appears to be from.</param>
        /// <param name="to">The "To" list.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="priority">The mail priority.</param>
        /// <param name="mailFormat">The mail format.</param>
        /// <param name="attachmentPath">The full path of an attachment.</param>
        public static void Send(string smtpServer, string from, string to, string subject, string body, MailFormat mailFormat, MailPriority priority, string attachmentPath)
        {
            Send(smtpServer, from, new[] { to }, subject, body, mailFormat, priority, attachmentPath);
        }

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="smtpServer">The SMTP server.</param>
        /// <param name="from">Who the message appears to be from.</param>
        /// <param name="to">The "To" list.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="priority">The mail priority.</param>
        /// <param name="mailFormat">The mail format.</param>
        public static void Send(string smtpServer, string from, ICollection<string> to, string subject, string body, MailFormat mailFormat, MailPriority priority)
        {
            Send(smtpServer, from, to, subject, body, mailFormat, priority, null);
        }

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="smtpServer">The SMTP server.</param>
        /// <param name="from">Who the message appears to be from.</param>
        /// <param name="to">The "To" list.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="priority">The mail priority.</param>
        /// <param name="mailFormat">The mail format.</param>
        /// <param name="attachmentPath">The full path of an attachment.</param>
        public static void Send(string smtpServer, string from, ICollection<string> to, string subject, string body, MailFormat mailFormat, MailPriority priority, string attachmentPath)
        {
            Guard.ArgumentNotNullOrEmptyString(smtpServer, "smtpServer");
            Guard.ArgumentNotNullOrEmptyString(from, "from");
            Guard.ArgumentNotNullOrEmpty(to, "to");
            Guard.ArgumentNotNullOrEmptyString(subject, "subject");
            Guard.ArgumentNotNullOrEmptyString(body, "body");

            MailMessage objMail = new MailMessage();

            objMail.From = new MailAddress(from);
            objMail.Subject = subject;
            objMail.Body = body;
            
            foreach (string toAddress in to)
            {
                objMail.To.Add(toAddress);
            }

            objMail.Priority = priority;
            objMail.IsBodyHtml = (mailFormat == MailFormat.Html);

            if (!string.IsNullOrEmpty(attachmentPath))
            {
                objMail.Attachments.Add(new Attachment(attachmentPath));
            }

            SmtpClient SmtpMail = new SmtpClient(smtpServer);
            SmtpMail.Send(objMail);
        }
    }
}